package cn.zebra.dev.admin.module.sys.controller;

import cn.zebra.dev.admin.module.sys.dto.ConfigDTO;
import cn.zebra.dev.admin.module.sys.vo.ConfigVO;
import cn.zebra.dev.common.constant.ParamsConstant;
import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.sys.module.config.entity.SysConfigEntity;
import cn.zebra.dev.sys.module.config.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author runnable@sina.cn
 */
@Api(tags = "系统配置")
@RestController
@RequestMapping("/config")
public class ConfigController {

    @Autowired
    private SysConfigService configService;

    @GetMapping("/list")
    @ApiOperation("系统配置列表")
    public Result<PageUtils<ConfigDTO>> list(
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page) {
        Map<String, Object> params = new HashMap<>(2);
        params.put(ParamsConstant.PAGE, page);
        params.put(ParamsConstant.LIMIT, limit);
        PageUtils<SysConfigEntity> pageUtils = configService.findPage(params);
        PageUtils<ConfigDTO> data = new PageUtils<>();
        List<SysConfigEntity> list = pageUtils.getList();
        if (list.size() > 0) {
            List<ConfigDTO> dtoList = new LinkedList<>();
            list.forEach(entity -> {
                ConfigDTO dto = new ConfigDTO();
                BeanUtils.copyProperties(entity, dto);
                dtoList.add(dto);
            });
            data.setList(dtoList);
        }
        return Result.success(data);
    }

    @GetMapping("/info/{id}")
    @ApiOperation("系统配置详情")
    public Result<ConfigDTO> info(@PathVariable Long id) {
        SysConfigEntity entity = configService.findById(id);
        ConfigDTO data = new ConfigDTO();
        BeanUtils.copyProperties(entity, data);
        return Result.success(data);
    }

    @PostMapping("/update/{id}")
    @ApiOperation("编辑系统配置")
    public Result update(@PathVariable Long id, @Valid @RequestBody ConfigVO vo) {
        SysConfigEntity sysConfigEntity = new SysConfigEntity();
        BeanUtils.copyProperties(vo, sysConfigEntity);
        return configService.updateConfig(id, sysConfigEntity) ? Result.success("编辑系统配置成功") : Result.error("编辑系统配置失败");
    }

    @PostMapping("/save")
    @ApiOperation("添加系统配置")
    public Result save(@Valid @RequestBody ConfigVO vo) {
        SysConfigEntity sysConfigEntity = new SysConfigEntity();
        BeanUtils.copyProperties(vo, sysConfigEntity);
        return configService.saveConfig(sysConfigEntity) ? Result.success("添加系统配置成功") : Result.error("添加系统配置失败");
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("删除系统配置")
    public Result delete(@PathVariable Long id) {
        return configService.deleteConfig(id) ? Result.success("删除成功") : Result.error("删除失败");
    }
}
